#pragma once

#include <qthread.h>
#include <qqueue.h>
#include <qpair.h>
#include <qstack.h>
#include <qmutex.h>

class QProcess;

class SimulateThread : public QThread
{
	Q_OBJECT

private:
	QString engine1;
	QString engine2;
	int times;
	int mode;
	int first;
	

	QProcess * e1;
	QProcess * e2;
	int turn = 1;
	int nowFirst = 0;
	int lastMove = -1;
	bool turnFlag = false;
	QQueue<QPair<QString, int>> cmdQueue;
	QMutex queueMutex;
	int board[15][15];
	int d[6][2] = { {-1, 1}, {0, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, 0} };

	void writeProcess(QProcess * process, const QString & data);
	int isGameOver();

public:
	SimulateThread(QObject * parent);
	~SimulateThread();

	virtual void run() override;

	void setParam(QString & engine1, QString & engine2, int times, int mode, int first);

private slots:
	void receiveEngine1Data();
	void receiveEngine2Data();

signals:
	void makeMove(int pos, int player);
	void endOfOneGame();
};